package android.olsson.apk;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import android.content.Context;
import android.olsson.apk.interfaces.IBeverage;
import android.view.ViewGroup;
import android.widget.TextView;

public class Util {

	public static float getAlcoholAsFloat(String alcohol) {
		alcohol = alcohol.replace("%", "");
		if (alcohol.contains(","))
			alcohol = alcohol.replace(",", ".");
		return Float.parseFloat(alcohol);
	}

	public static float getPriceAsFloat(String price) {
		return Float.parseFloat(price);
	}

	public static float getVolumeAsFloat(String volume) {
		return Float.parseFloat(volume);
	}

	public static ArrayList<IBeverage> getSortedBeverageList(String type,
			ArrayList<IBeverage> beverageList) {
		Collections.sort(beverageList, new Comparator<IBeverage>() {

			public int compare(IBeverage b1, IBeverage b2) {
				IBeverage beverage1 = (IBeverage) b1;
				IBeverage beverage2 = (IBeverage) b2;

				return beverage1.compareTo(beverage2);
			}
		});

		Collections.reverse(beverageList);

		if (!(type.equalsIgnoreCase("Alla"))) {
			for (int i = 0; i < beverageList.size(); i++) {
				if (!(beverageList.get(i).getType().equalsIgnoreCase(type))) {
					beverageList.remove(i);
				}
			}
		}
		return beverageList;
	}

	// Not used at the moment but might be useful in the future
	public static ArrayList<IBeverage> reverseBeverageList(
			ArrayList<IBeverage> beverageList) {
		Collections.reverse(beverageList);
		return beverageList;
	}
	// --------------------------------------------------------

	// Draws the beverages in the given layout (ViewGroup is just a higher abstraction).
	// Note to user: If the type isn't important then set it as 'Alla'.
	public static void displayBeverages(Context context,
			ArrayList<IBeverage> beverageList, ViewGroup viewGroup, String type) {
		for (int i = 0; i < beverageList.size(); i++) {
			IBeverage beverage = beverageList.get(i);
			int index = i + 1;
			String result = index + ". " + beverage.getName() + "\n";
			if (type.equalsIgnoreCase("Alla")) {
				result += beverage.getType() + "\n";
			}
			result += beverage.getApk() 	+ " apk\n"
					+ beverage.getVolume() 	+ " ml\n"
					+ beverage.getAlc() 	+ " %\n"
					+ beverage.getPrice() 	+ " kr\n";

			TextView textView = new TextView(context);
			textView.setTextAppearance(context,
					android.R.style.TextAppearance_Medium);
			textView.setText(result);

			viewGroup.addView(textView);
		}
	}
}
